package com.example.mystore.servlets.ctrl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

public class StepServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String inDo = req.getServletPath();
        String toURL = "/Cart.jsp";
//        四个步骤结算地址信息支付信息支付
        HashMap<String ,String> orderMassage = (HashMap<String, String>) req.getSession().getAttribute("orderMassage");
        if(orderMassage == null) {
            orderMassage = new HashMap<>();
        }
//        合计价格payMassage页面
        if (inDo.equals("/Settlement")){
            String FinalPrice = req.getParameter("FinalPrice");
            req.getSession().setAttribute("FinalPrice",FinalPrice);
            toURL = "/address.jsp";
        }
        if (inDo.equals("/Address")){
            String name = req.getParameter("name");
            String address = req.getParameter("address");
            String PAC = req.getParameter("PAC");
            String phone = req.getParameter("tel");
            String fax = req.getParameter("fax");
            orderMassage.put("name",name);
            orderMassage.put("address",address);
            orderMassage.put("PAC",PAC);
            orderMassage.put("phone",phone);
            orderMassage.put("fax",fax);
            req.getSession().setAttribute("orderMassage",orderMassage);
            toURL = "/payMassage.jsp";
        }
        if (inDo.equals("/PayMassage")){
            String card_name = req.getParameter("card_name");
            String card_number = req.getParameter("card_number");
            String bill_address = req.getParameter("bill_address");
            String closing_date = req.getParameter("closing_date");
            String card_type = req.getParameter("card_type");
            orderMassage.put("card_name",card_name);
            orderMassage.put("card_number",card_number);
            orderMassage.put("bill_address",bill_address);
            orderMassage.put("closing_date",closing_date);
            orderMassage.put("card_type",card_type);
            req.getSession().setAttribute("orderMassage",orderMassage);
            toURL = "/order.jsp";
        }
        if(inDo.equals("/Pay")){
            toURL = "/IOrderS";
        }
        resp.sendRedirect(req.getContextPath()+toURL);



    }


    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }
}
